/***
 * author:jyh
 * edit页面基类
 * formId：对应的表单ID
 * objId:list页面传过来的对象ID
 * idProperty:对象的ID属性名
 * objName:list页面传过来的对象名称
 * moduleName:模块名称,用来构造新增、修改、查看的jsp文件名,如：addRole.jsp,editRole.jsp,viewRole.jsp中的Role
 * queryHandler:查询handler名
 * queryMethod：查询表单内容的方法名称，默认为getEntity
 * dataSetHandler：datasethander名称
 * saveMethod：保存对象的方法名，默认为save
 * initFun:构造完后，调用的方法,默认为null，不调用
 */

Ext.define('Pearlsen.BaseEdit',{
	formId:null,
	idProperty:null,//model中的ID字段
	objId:null,
	objName:null,
	moduleName:null,
	queryHandler:null,
	queryMethod:'getEntity',
	dataSetHandler:null,
	saveMethod:'save',
	initFun:null,
	_form:null,
	constructor : function(config){
		this.objId = this.getQueryString("objId");
		this.objName = this.getQueryString("objName");
		this.moduleName = config.moduleName;
		this.queryMethod = config.queryMethod ? config.queryMethod:'getEntity';
		this.queryHandler = config.queryHandler;
		this.dataSetHandler = config.dataSetHandler;		
		this.saveMethod = config.saveMethod ? config.saveMethod:'save';
		if( Ext.isEmpty(config.formId) ){
			throw new Error("没有设置formId");
		}
		if( Ext.isEmpty(config.idProperty) ){
			throw new Error("没有设置idProperty");
		}
		Ext.apply(this,config,true);
		if(this.initFun && typeof this.initFun=='function'){
			this.initFun.call(this);
		}
	},
	getForm:function(){
		if( this._form ==null){
			this._form = Ext.getCmp(this.formId).getForm();
		}
		return this._form;
	},
	getQueryString:function(paramName){
		var regexp = new RegExp("(^|&)"+paramName+"=([^&]*)(&|$)");
		var result = window.location.search.substr(1).match(regexp); 
		if( result ){
			return decodeURI(result[2]);
		}else{
			return null;
		}
	},
	loadForm:function(){
		 var query = new Query(this.queryHandler,"_load_dataset");
		 query.setHandlerMethod(this.queryMethod);		 
		 query.setURL('/query.asp');
		 query.addCondition("objId", this.objId);
		 query.setCompletedFun(this.afterloadForm);
		 query.setCompletedFunScope(this);
		 query.load();
	},
	afterloadForm:function(success,dataset,response, opts){
		if(success){
			if( dataset && !Ext.isEmpty(dataset.record)){
				this.getForm().setValues(dataset.record[0]);
			}
		}else{
			
		}
	},
	submitForm:function(){
		if(this.getForm().isValid()){
			var me = this;
	    	var fields = this.getForm().getValues();
	    	var dataSet = new DataSet(this.dataSetHandler,"_save_dataset");
	    	dataSet.setHandlerMethod(this.saveMethod);
	    	dataSet.newRecord();
	    	Ext.iterate(fields,function(name,value){
	    		dataSet.setValue(name,value);
	    	});
	    	dataSet.execute(function(result){
	    		if(result.getStatus()=="200"){
	    			Pearlsen.Utils.showSuccessMsg('保存成功');
//	    			if( result.getRecords() && result.getRecords().length > 0 ){
//		    			var record = result.getRecords()[0];
//		    			me.objId = record[me.idProperty];
//		    			if( me.objId ){
//		    				me.loadForm();
//		    			}
//	    			}
	    			GlobalEvent.fireEvent(me.moduleName+"_refresh_list");
	    			me.close();
	    			return;
	    		}else{
	    			Pearlsen.Utils.showErrorMsg('保存失败,原因:'+result.message);
	    			return ;
	    		}
	    	});	
		}
	},
	close:function(){
		var tabId = this.getTabId();
		parent.MainTab.removeTab(tabId);
	},
	createTabId : function(ops, objId) {
		var tabId = this.getTabId() + '_' + ops;
		if (objId)
			tabId += objId;
		return tabId;
	},
	getTabId : function() {
		if (this.tagId)
			return this.tagId;
		if (frameElement.attributes.tabid)
			return frameElement.attributes.tabid.value;
		else
			throw new Error("标签页中未定义tabid属性");
	}

});